package com.rq.demo.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.rq.base.ui.BaseController;
import com.rq.base.ui.CtrWatcher;
import com.rq.base.ui.impl.CtrListener;
import com.rq.base.ui.impl.CtrTag;
import com.rq.base.ui.impl.OnClick;
import com.rq.base.util.LOG;
import com.rq.base.util.StringUtil;
import com.rq.demo.R;

public class Fra3 extends BaseController implements View.OnClickListener, CtrListener {
    @Override
    public int getLayoutId() {
        return R.layout.fra_3;
    }

    private String pageTag = "";

    @Override
    public void onViewCreated(Bundle savedInstanceState) {
        super.onViewCreated(savedInstanceState);
        pageTag = getPass(0);
        setData2View(R.id.tv_show, pageTag);
        if ("10".equals(pageTag)) {
            setData2View(R.id.btn5, View.VISIBLE);
        }
    }

    public static final String ACTION_PASS = "pass";
    public static final int RES_WAIT = 11;

    @Override
    protected boolean onResultOK(Intent data) {
        if (isBackWith(RES_WAIT, data)) {
            String passFromFra1 = getPass(0, data);
            LOG.e("Fra3", "43:" + passFromFra1);
            setData2View(R.id.tv_show, getInput(R.id.tv_show) + "\nback:" + StringUtil.getTimeFromNet(passFromFra1));
            return true;
        }
        return super.onResultOK(data);
    }

    @Override
    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn5})
    public void onClick(View v) {
        if (v.getId() == R.id.btn1) {
            if (getOperator() != null) {
                LOG.e("Fra3", "31:");
                getOperator().openWith(this, Fra1.class, RES_WAIT, (String) getPass(0));
            } else {
                LOG.e("Fra3", "32:");
            }
        } else if (v.getId() == R.id.btn2) {
            open(Fra2.class, (String) getPass(0));
        } else if (v.getId() == R.id.btn3) {
            finish();
        } else if (v.getId() == R.id.btn5) {
            CtrWatcher.get().post(ACTION_PASS, System.currentTimeMillis());
        }
    }

    @Override
    @CtrTag({ACTION_PASS})
    public void tagInfo(String tag, Object[] object) {
        if (ACTION_PASS.equals(tag) && "3".equals(this.pageTag)) {
            setData2View(R.id.tv_show, getInput(R.id.tv_show) + "--" + object[0]);
        }
    }
}
